import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.TypedValue
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.tangcl.module_commom.R
import java.lang.Exception

/**
 * Created by TCL on 2021/5/18.
 * Describe:弹窗基类
 */
abstract class BaseDialogFragment : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化数据
        initData(arguments)
        //设置自定义样式
        setStyle(setDialogStyle(), setDialogTheme())
        //设置返回键
        isCancelable = setCancel()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(getLayoutId(), container)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        if(isImmersionBarEnabled()){
//            initImmersionBar()
//        }
        initView(view)
        setListener()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val mDialog = dialog
        if(mDialog!=null){
            mDialog.setCanceledOnTouchOutside(setCancelOnTouchOutside())
            val mWindow = mDialog.window
            if(mWindow!=null){
                mWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                setWindowType(mWindow)
                if(setWidth() == WindowManager.LayoutParams.MATCH_PARENT){
                    val leftAndRight:Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, setLeftRightPadding(), Resources.getSystem().displayMetrics).toInt()
                    mWindow.decorView.setPadding(leftAndRight,0,leftAndRight,0)
                }
                if(isShowAnimation()){
                    mWindow.setWindowAnimations(setWindowAnimationsStyle())
                }
                val wlp = mWindow.attributes
                wlp.gravity = setGravity()
                wlp.dimAmount = setBgAlpha()
                wlp.width = setWidth()
                wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
                mWindow.attributes = wlp
            }
        }
    }

    /**
     * 是否启用ImmersionBar
     */
    protected open fun isImmersionBarEnabled(): Boolean {
        return false
    }

//    /**
//     * 初始化ImmersionBar
//     */
//    protected open fun initImmersionBar() {
//        immersionBar {
//            statusBarColor(R.color.custom_white)
//        }
//    }

    /**
     * 设置弹出框样式
     */
    protected open fun setDialogStyle(): Int {
        return STYLE_NO_TITLE
    }

    /**
     * 设置弹出框主题
     */
    protected open fun setDialogTheme(): Int {
        return R.style.Theme_AppCompat_Dialog
    }

    /**
     * 设置返回键
     */
    protected open fun setCancel(): Boolean {
        return true
    }

    /**
     * 设置返回键
     */
    protected open fun isShowAnimation(): Boolean {
        return true
    }

    /**
     * 设置空白区域
     */
    protected open fun setCancelOnTouchOutside(): Boolean {
        return true
    }

    /**
     * 设置窗口宽
     */
    protected open fun setWidth(): Int {
        return WindowManager.LayoutParams.MATCH_PARENT
    }

    /**
     * 设置左右间距
     */
    protected open fun setLeftRightPadding(): Float {
        return 0f
    }

    /**
     * 设置窗口转场动画
     */
    protected open fun setWindowAnimationsStyle(): Int {
        return R.style.Animation_Design_BottomSheetDialog
    }

    /**
     * 设置Dialog gravity
     */
    protected open fun setGravity(): Int {
        return Gravity.CENTER
    }

    /**
     * 设置Dialog 透明度
     */
    protected open fun setBgAlpha(): Float {
        return 0.4f
    }

    /**
     * 设置Dialog gravity
     */
    protected open fun setDialogType(): Int {
        return 0
    }

    /**
     * 自定义时添加layout
     */
    abstract fun getLayoutId(): Int

    /**
     * 加载数据
     *
     * @param bundle 用这个Bundle对象接收传入时的Bundle对象
     */
    protected abstract fun initData(bundle: Bundle?)


    /**
     * 初始化
     */
    abstract fun initView(view: View)

    /**
     * 监听
     */
    protected open fun setListener() {
    }

    fun isDialogShowing(): Boolean {
        return null != dialog && dialog!!.isShowing
    }

    private fun setWindowType(mWindow: Window) {
        when(setDialogType()){
            1->{
                mWindow.setType(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW)
            }
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            //在每个add事务前增加一个remove事务，防止连续的add
            manager.beginTransaction().remove(this).commit()
            super.show(manager, tag)
        } catch (e: Exception) {
            //同一实例使用不同的tag会异常,这里捕获一下
            e.printStackTrace()
        }
    }

}